import Vue from "vue";
import VueRouter from "vue-router";
// import Login from "../components/MyLogin.vue";
// import Home from "../components/MyHome.vue";
// import Welcome from "../components/Welcome.vue";
// import Users from "../components/user/UserList.vue";
// import Rights from "../components/power/Rights.vue";
// import Roles from "../components/power/Roles.vue";
// import Cate from "../components/goods/Cate.vue";
// import Params from "../components/goods/Params.vue";
const Login = (resolve) => {
  import("../components/MyLogin.vue").then((module) => {
    resolve(module);
  });
};
const Home = (resolve) => {
  import("../components/MyHome.vue").then((module) => {
    resolve(module);
  });
};
const Welcome = (resolve) => {
  import("../components/Welcome.vue").then((module) => {
    resolve(module);
  });
};
const Users = (resolve) => {
  import("../components/user/UserList.vue").then((module) => {
    resolve(module);
  });
};
const Rights = (resolve) => {
  import("../components/power/Rights.vue").then((module) => {
    resolve(module);
  });
};
const Roles = (resolve) => {
  import("../components/power/Roles.vue").then((module) => {
    resolve(module);
  });
};
const Cate = (resolve) => {
  import("../components/goods/Cate.vue").then((module) => {
    resolve(module);
  });
};
const Params = (resolve) => {
  import("../components/goods/Params.vue").then((module) => {
    resolve(module);
  });
};
const List = (resolve) => {
  import("../components/goods/List.vue").then((module) => {
    resolve(module);
  });
};
const AddGoods = (resolve) => {
  import("../components/goods/AddGoods.vue").then((module) => {
    resolve(module);
  });
};
const Order = (resolve) => {
  import("../components/order/Order.vue").then((module) => {
    resolve(module);
  });
};
const Report = (resolve) => {
  import("../components/report/Report.vue").then((module) => {
    resolve(module);
  });
};
import axios from "axios";

Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome },
      { path: "/users", component: Users },
      { path: "/rights", component: Rights },
      { path: "/roles", component: Roles },
      { path: "/categories", component: Cate },
      { path: "/params", component: Params },
      { path: "/goods", component: List },
      { path: "/goods/add", component: AddGoods },
      { path: "/orders", component: Order },
      { path: "/reports", component: Report },
      { path: "/reports", component: Report },
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  routes,
});
// 判断本地存储是否有 token
router.beforeEach((to, from, next) => {
  if (to.path == "/login") return next();
  const tok = window.sessionStorage.getItem("token");
  if (!tok) return next("/login");
  next();
});
// 请求拦截
axios.interceptors.request.use((config) => {
  config.headers.Authorization = window.sessionStorage.getItem("token");
  return config;
});
export default router;
